﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMove : MonoBehaviour
{
    #region use Input Manager
    private readonly string mJoyStickXName = "JoyStickX";
    private readonly string mJoyStickYName = "JoyStickY";
    private readonly string mJoyStickZXName = "JoyStickZX";
    private readonly string mJoyStickZYName = "JoyStickZY";
    #endregion

    #region use Input System
    private Vector2 mJoyStickLeft2;
    private Vector3 mJoyStickLeft3;
    private Vector2 mJoyStickRight2;
    private Keyboard mKeyborad = Keyboard.current;
    private Gamepad mGamePad = Gamepad.current;
    #endregion

    private Vector3 v = Vector3.zero;
    private float mMoveSpeed = 0.025f;
    private float mTurnAngle = 0.0f;
    private float mLastTurnAngle = 0.0f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        /*
        if (Input.GetKey(KeyCode.UpArrow)) this.transform.position += new Vector3(0, 0, 0.1f);
        if (Input.GetKey(KeyCode.DownArrow)) this.transform.position += new Vector3(0, 0, -0.1f);
        if (Input.GetKey(KeyCode.LeftArrow)) this.transform.position += new Vector3(-0.1f, 0, 0);
        if (Input.GetKey(KeyCode.RightArrow)) this.transform.position += new Vector3(0.1f, 0, 0);
        */
        if(mKeyborad == null)
        {
            if (Input.GetKey(KeyCode.UpArrow)) this.transform.position += new Vector3(0, 0, 0.1f);
            if (Input.GetKey(KeyCode.DownArrow)) this.transform.position += new Vector3(0, 0, -0.1f);
            if (Input.GetKey(KeyCode.LeftArrow)) this.transform.position += new Vector3(-0.1f, 0, 0);
            if (Input.GetKey(KeyCode.RightArrow)) this.transform.position += new Vector3(0.1f, 0, 0);
            if (Input.GetKeyDown(KeyCode.Space))
            {
                var _light = this.GetComponentInChildren<Light>();
                if (_light.enabled) _light.enabled = false;
                else _light.enabled = true;
            }
        }
        if(mGamePad == null)
        {
            // position
            var _xl = Input.GetAxis(mJoyStickXName);
            var _yl = Input.GetAxis(mJoyStickYName);
            var _JoyStickLeftInput = new Vector2(_xl, _yl);
            var _JoyStickLeftInput3 = SquareToCircle(_JoyStickLeftInput);
            var _currSpeed = mMoveSpeed;
            // boost
            //_currSpeed = (1f + Input.GetAxis("Boost")) * mMoveSpeed;
            this.transform.Translate(_JoyStickLeftInput3 * _currSpeed, Space.Self);

            // rotation
            var _xr = Input.GetAxis(mJoyStickZXName);
            var _yr = Input.GetAxis(mJoyStickZYName);
            if (Mathf.Sqrt(Mathf.Pow(_xr, 2) + Mathf.Pow(_yr, 2)) > 0.35f)
            {
                mTurnAngle = TurnAngle(new Vector2(_xr, _yr));
                this.transform.Rotate(new Vector3(0f, mTurnAngle - mLastTurnAngle, 0f));
                mLastTurnAngle = mTurnAngle;
            }
            else
            {
                mLastTurnAngle = 0.0f;
            }

            // light switch
            if (Input.GetKeyDown(KeyCode.Joystick1Button0))
            {
                var _light = this.GetComponentInChildren<Light>();
                if (_light.enabled) _light.enabled = false;
                else _light.enabled = true;
            }
        }
    }

    private Vector3 SquareToCircle(Vector2 temp)
    {
        Vector3 output = Vector3.zero;
        output.x = temp.x * Mathf.Sqrt(1 - (temp.y * temp.y) / 2);
        output.z = temp.y * Mathf.Sqrt(1 - (temp.x * temp.x) / 2);
        return output;
    }

    private float TurnAngle(Vector2 temp)
    {
        var _turnAngle = 0.0f;
        var _x = temp.x;
        var _y = temp.y;
        // Atan return by rad.
        _turnAngle = Mathf.Atan(_x / _y);
        // x>0, y>0
        // x>0, y<0:pi + angle
        // x<0, y>0
        // x<0, y<0:angle - pi
        // x>0, y=0:pi/2
        // x<0, y=0:-pi/2
        if (_x >= 0 && _y < 0) _turnAngle = _turnAngle + Mathf.PI;
        else if (_x < 0 && _y < 0) _turnAngle = _turnAngle - Mathf.PI;
        else if (_x >= 0 && _y == 0) _turnAngle = Mathf.PI / 2;
        else if (_x <= 0 && _y == 0) _turnAngle = -Mathf.PI / 2;
        _turnAngle = Mathf.Rad2Deg * _turnAngle;
        return _turnAngle;
    }
}
